package com.duolebo.uteped_sdk.msg;

import androidx.annotation.NonNull;

import com.duolebo.uteped_sdk.UTEPedEventHandler;
import com.duolebo.uteped_sdk.utils.Const;
import com.yc.nadalsdk.bean.Response;
import com.yc.nadalsdk.bean.TimeClock;
import com.yc.nadalsdk.ble.open.UteBleConnection;

import java.util.HashMap;
import java.util.Map;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class TimeMessage extends BaseMessage {
    private final static String PATH = "time";
    /**
     * BaseCallback
     *
     * @param methodChannel methodChannel
     * @param eventHandler eventHandler
     */
    public TimeMessage(MethodChannel methodChannel, UTEPedEventHandler eventHandler) {
        super(methodChannel, eventHandler);
    }

    @Override
    public String getPath() {
        return PATH;
    }

    @Override
    public boolean methodCallConsumed(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        if (!call.hasArgument(Const.KEY_METHOD))
            return false;

        if (super.methodCallConsumed(call, result))
            return true;

        UteBleConnection uteBleConnection = uteBleClient.getUteBleConnection();
        boolean consumed = true;
        String method = call.argument(Const.KEY_METHOD);
        assert method != null;

        boolean enable;
        Response<?> rsp;
        Map<String, Object> map = new HashMap<>();
        boolean success = false;
        switch (method) {

            case Const.M_setTimeClock:
                if (uteBleClient.isConnected()) {
                    rsp = uteBleConnection.setTimeClock(getTimeClock());
                    success = rsp.getErrorCode() == Const.CODE_SUCCESS;
                }
                result.success(success);
                break;

            case Const.M_getTimeClock:
                if (uteBleClient.isConnected()) {
                    Response<TimeClock> timeClockRsp = uteBleConnection.getTimeClock();
                    map = objectToMap(timeClockRsp, true, false);
                }
                result.success(map);
                break;

            case Const.M_queryWorldClock:
                if (uteBleClient.isConnected()) {
                    uteBleConnection.queryWorldClock();
                }
                break;

            case Const.M_querySupportWorldClockCount:
                int count = 0;
                if (uteBleClient.isConnected()) {
                    Response<Integer> countRsp = uteBleConnection.querySupportWorldClockCount();
                    count = countRsp.isSuccess() ? countRsp.getData() : 0;
                }
                result.success(count);
                break;

            case Const.M_setWorldClock:
                break;

            default:
                consumed = false;
        }

        return consumed;
    }
}
